#define _CRT_SECURE_NO_WARNINGS
#include "string.h"

namespace bit
{
	//class string
	//{
	//public:
	//	int size()
	//	{
	//		return _finish;
	//	}
	//};

	string::iterator string::begin()
	{
		return _start;
	}
	string::iterator string::end()
	{
		return _finish;
	}

	size_t string::size()const
	{
		return _finish-_start;
	}
	size_t string::capacity()const
	{
		return _end_of_sortage-_start;
	}

	void string::reserve(size_t n)
	{
		if (n > capacity())
		{
			int oldsize = size();
			char* tmp = new char[n];
			strcpy(tmp, _start);
			delete[] _start;

			_start = tmp;
			_finish = _start + oldsize;
			_end_of_sortage = _start + n;

		}
	}

	char* string::c_str()
	{
		return _start;
	}

	string& string::operator=(const char* s)
	{
		size_t size = strlen(s);
		_start = new char[size + 1];
		strcpy(_start, s);
		_finish = _start + size;
		_end_of_sortage = _finish+1;
		return *this;
	}
	string& string::operator=(const string& s)
	{
		_start = new char[s.capacity() + 1];
		strcpy(_start, s._start);
		_finish = _start + s.size();
		_end_of_sortage = _start + s.capacity();
		return *this;
	}
	char& string::operator[](size_t pos)
	{
		assert(pos < 0);
		assert(pos >= size());
		return _start[pos];
	}

	string& string::operator+=(const char* s)
	{
		int len = strlen(s);
		if (capacity() < len+size())
		{
			reserve(len + size() + 1);
		}
		strcat(_finish, s);
		_finish = _finish + len;
		return *this;
	}

	string& string::append(const char* s)
	{
		*this += s;
		return *this;
	}

	char string::push_back()
	{
		return *(_finish - 1);
	}

	string& string::assign(char* s)
	{
		int len = strlen(s);
		if (len > capacity())
		{
			reserve(len + 1);
		}
		strcpy(_start, s);
		_finish = _start + len;
		return *this;
	}

	string& string::insert(size_t pos, const char* s)
	{
		int len = strlen(s);
		if (size() + len >= capacity())
		{
			reserve(size() + len + 1);
		}
		int end = size() + len;
		while (end-len>=pos)
		{
			_start[end--] = _start[end-len];
		}
		memcpy(_start + pos, s, len);
		_finish = _finish + len;
		return *this;
	}
	string& string::erase(size_t pos, size_t len)
	{
		if (pos + len - 1 >= size())
		{
			_start[pos] = '\0';
			_finish = _start + pos;
			return *this;
		}
		strcpy(_start + pos, _start + pos + len);
		_finish -= len;
		return *this;
	}

	void string::swap(string& s)
	{
		std::swap(_start, s._start);
		std::swap(_finish, s._finish);
		std::swap(_end_of_sortage, s._end_of_sortage);
	}

}

int main()
{
	bit::string s("hello world");
	bit::string s1("aaaaa");
	s.swap(s1);
	cout << s.c_str() << endl;
	cout << s1.c_str() << endl;

	//s.insert(7, "aaa");
	//cout << s.c_str() << endl;
	//cout << s.size() << endl;
	//cout << s.capacity() << endl;
	//s.erase(2, 3);
	//cout << s.c_str() << endl;
	//cout << s.size() << endl;
	//cout << s.capacity() << endl;
	//s += " aaa";
	//cout << s.c_str() << endl;
	//cout << s.size() << endl;
	//cout << s.capacity() << endl;
	////bit::string::iterator it = s.begin();
	//while (it != s.end())
	//{
	//	cout << *it;
	//	it++;
	//}
	//cout << endl;
	//for (auto e : s)
	//{
	//	cout << e;
	//}
	//cout << endl;
	//cout << s.c_str() << endl;
	//cout << s.size() << endl;
	//cout << s.capacity() << endl;
	//s.reserve(100);
	//cout << s.c_str() << endl;
	//cout << s.size() << endl;
	//cout << s.capacity() << endl;
	//bit::string s1(s);
	//cout << s1.c_str() << endl;
	//cout << s1.size() << endl;
	//cout << s1.capacity() << endl;
}